package com.example.hello.control;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserControl {

    // 假数据库：key=名字，value=年龄
    private static final Map<String, Integer> nameAges = new HashMap<>();

    /* 1. 查所有 */
    @GetMapping
    public Map<String, Integer> list() {
        return nameAges;   // Postman 直接看到 JSON
    }

    /* 2. 增 */
    @PostMapping
    public String helloPost(@RequestParam String name,
                            @RequestParam int age) {
        nameAges.put(name, age);
        return "add name: " + name + ", age= " + age;
    }

    /* 3. 改 */
    @PutMapping
    public String helloPut(@RequestParam String name,
                           @RequestParam int age) {
        if (!nameAges.containsKey(name)) {
            return "name not exist";
        }
        nameAges.replace(name, age);
        return "update name: " + name + ", age= " + age;
    }

    /* 4. 删 */
    @DeleteMapping
    public String helloDel(@PathVariable String name) {
        if (nameAges.remove(name) == null) {
            return "name not exist";
        }
        return "delete name: " + name;
    }
}